//////////////////////////////////////////////////////////////////////////////////////
//
//  Copyright (c) 2014-present, Egret Technology.
//  All rights reserved.
//  Redistribution and use in source and binary forms, with or without
//  modification, are permitted provided that the following conditions are met:
//
//     * Redistributions of source code must retain the above copyright
//       notice, this list of conditions and the following disclaimer.
//     * Redistributions in binary form must reproduce the above copyright
//       notice, this list of conditions and the following disclaimer in the
//       documentation and/or other materials provided with the distribution.
//     * Neither the name of the Egret nor the
//       names of its contributors may be used to endorse or promote products
//       derived from this software without specific prior written permission.
//
//  THIS SOFTWARE IS PROVIDED BY EGRET AND CONTRIBUTORS "AS IS" AND ANY EXPRESS
//  OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
//  OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
//  IN NO EVENT SHALL EGRET AND CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
//  INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
//  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;LOSS OF USE, DATA,
//  OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
//  LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
//  NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
//  EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
//////////////////////////////////////////////////////////////////////////////////////

class Main extends egret.DisplayObjectContainer {



    public constructor() {
        super();
        this.addEventListener(egret.Event.ADDED_TO_STAGE, this.onAddToStage, this);
    }
    private Score: number = 0;
    private Scoretext: egret.TextField = new egret.TextField();
    private timer: egret.Timer = new egret.Timer(1000, 0)

    private onAddToStage(event: egret.Event) {

        var loader: egret.ImageLoader = new egret.ImageLoader();
        loader.addEventListener(egret.Event.COMPLETE, this.loadfun, this);
        loader.load("resource/assets/11.jpg");

        this.timer.addEventListener(egret.TimerEvent.TIMER, this.timerFunc, this);
        this.timer.start();
        this.addEventListener(egret.Event.ENTER_FRAME, this.Mainenfun, this)

    }
    private loadfun(e: egret.Event) {
        var bitmap: egret.Bitmap = new egret.Bitmap();
        var texture1: egret.Texture = new egret.Texture();
        texture1.bitmapData = e.target.data;
        bitmap.texture = texture1;
        this.addChild(bitmap);

    }


    private Mainenfun(e: egret.Event) {

        this.Scoretext.text = String(this.Score);
        this.addChild(this.Scoretext);

    }
    private timerFunc(e: egret.TimerEvent) {
        var bubble: MoveBubble = new MoveBubble();
        this.addChild(bubble);

        //  var p: number[] = [150, 300, 450];
        //   var pindex: number = Math.round(Math.random() * 2);
        //  bubble.x = p[pindex];
        bubble.x = Math.random() * 640;
        bubble.touchEnabled = true;
        bubble.addEventListener(egret.TouchEvent.TOUCH_TAP, touchfun, this);
        function touchfun(e: egret.TouchEvent) {
            this.Score += 1;
            this.removeChild(e.target);
            var sound: egret.Sound = new egret.Sound();
            sound.addEventListener(egret.Event.COMPLETE, function loadOver(event: egret.Event) {
                sound.play(0,1);
            }, this);
            sound.addEventListener(egret.IOErrorEvent.IO_ERROR, function loadError(event: egret.IOErrorEvent) {
                console.log("loaded error!");
            }, this);
            sound.load("resource/succeed.mp3");

        }
    }
}